package com.bdd.jpa.config;

import com.bdd.jpa.factory.JobFactory;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @Author Bdd
 * @Description
 *  配置Schedule信息
 * @Date 2019/9/20 16:58
 */
@Configuration
//开启定时功能
@EnableScheduling
public class TaskConfiguration {

    @Autowired
    private JobFactory jobFactory;

    /**
     *  创建定时任务对象，单例的
     * @return
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        //将job实例，并能够注入Spring管理的Bean
        schedulerFactoryBean.setJobFactory(jobFactory);
        return schedulerFactoryBean;
    }

    /**
     * 创建scheduler对象
     * @return
     */
    @Bean
    public Scheduler scheduler(){
        return schedulerFactoryBean().getScheduler();
    }

}
